package com.uchedao.seller.ui.car.publish.condition;

import android.content.ContentValues;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.nostra13.universalimageloader.utils.L;
import com.uchedao.seller.R;
import com.uchedao.seller.db.DbHelper;
import com.uchedao.seller.module.Condition;
import com.uchedao.seller.module.Draft;
import com.uchedao.seller.config.GlobalInfo;
import com.uchedao.seller.ui.car.adapter.AdviceAdapter;
import com.uchedao.seller.widget.loadstate.UCheDaoRelativeLayout;

/**
 * 保养建议
 */
public class ConditionAdviceView extends UCheDaoRelativeLayout {


    private Condition condition;

    public ConditionAdviceView(Context context) {
        super(context);
    }

    public ConditionAdviceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ConditionAdviceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean save() {
                ContentValues values = new ContentValues();
        values.put(DbHelper.KEY_DRAFT_CONDITION, toJson(condition));

        int status = condition.getStatus();
        values.put(DbHelper.KEY_DRAFT_CONDITION_STATUS, status);

        // 若VIN值为空
        if (TextUtils.isEmpty(condition.config_info.vin))
            condition.config_info.vin = GlobalInfo.draft.car_vin;

            // 如果VIN值有更改
        else if (!condition.config_info.vin.equals(GlobalInfo.draft.car_vin)) {
            values.put(DbHelper.KEY_DRAFT_CAR_VIN, condition.config_info.vin);
        }

        // int status = GlobalInfo.draft.status[Draft.TYPE_CONDITION];
        // // 若原先状态为未完成，且数据已变动，则状态改变为已完成
        // if (GlobalInfo.draft.status[Draft.TYPE_CONDITION] !=
        // Draft.STATUE_FINISH) {
        // values.put(DbHelper.KEY_DRAFT_CONDITION_STATUS, Draft.STATUE_FINISH);
        // status = Draft.STATUE_FINISH;
        // }

        // 保存成功
        if (GlobalInfo.dbHelper.updateDraf(GlobalInfo.draft.car_plate, values)) {
            GlobalInfo.draft.condition = condition.clone();
            GlobalInfo.draft.car_vin = condition.config_info.vin;
            GlobalInfo.draft.status[Draft.TYPE_CONDITION] = status;
            L.i(toJson(GlobalInfo.draft.condition));
            return true;
        }
        return false;
    }

    @Override
    public boolean dataChanged() {
        L.i(toJson(condition));
        L.i(toJson(GlobalInfo.draft.condition));
        return !toJson(condition).equals(toJson(GlobalInfo.draft.condition));
//        return false;
    }

    @Override
    public void initDataBeforView() {
//        if (GlobalInfo.draft == null) {
//            GlobalInfo.draft = new Draft();
//        }
        condition = GlobalInfo.draft.condition.clone();
    }

    @Override
    public void initView() {

        LayoutInflater.from(mContext).inflate(R.layout.view_condition_maintenance,this);
        TextView adviceSubjectTv = (TextView) findViewById(R.id.condition_mainten_title).findViewById(
                R.id.subject_tv);
        adviceSubjectTv.setText(R.string.condition_maintenance_advice);

        TextView noteSubjectTv = (TextView) findViewById(R.id.condition_mainten_note_title).findViewById(
                R.id.subject_tv);
        noteSubjectTv.setText(R.string.condition_maintenance_note);

        EditText noteEt = (EditText) findViewById(R.id.note_mainten_note_et);
        noteEt.setText(condition.note);
        noteEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 检测说明
                condition.note = s.toString();
            }
        });
        noteEt.clearFocus();
        GridView maintenGv = (GridView) findViewById(R.id.condition_mainten_gv);
        maintenGv.setAdapter(new AdviceAdapter(mContext, condition.maintenance));

    }

    @Override
    public void initDataAfterView() {

    }

    @Override
    public void initListener() {

    }
}
